<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Travel extends MY_Controller {
  function __construct(){
    parent::__construct();
  }
  function index(){

		$this->_fetch("index");
  }
  function admin_index(){
    $this->_assign("adminIndex", TRUE);
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $this->load->model('travel_model','travelModel');
    $where["order"] = "pos_cr_date desc";
    $where["where"]["status"] = "1";
    $where["where"]["img_firstimage"] = "1";
    $travelResult = $this->travelModel->getImage($where);
    $this->_assign("traList",$travelResult);
    $this->_fetch("admin_index"); 
  }
   function admin_upload(){
    $this->_assign("adminIndex", TRUE);
    $this->load->model('category_model','categoryModel');
    $where["where"]["parent_id"] = "2";
    $travelResult = $this->categoryModel->get($where);
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $this->load->library('googlemaps');
    $config['center'] = '7.966598, 98.359929';
    $config['zoom'] = '10';
    $config['map_height'] = '300px';
    $config['map_width'] = '450px';
    $this->googlemaps->initialize($config);

    $marker = array();
    $marker['position'] = '7.966598, 98.359929';
    $marker['draggable'] = true;
    $marker['ondragend'] = 'addValue(event.latLng.lat(), event.latLng.lng());';
    $this->googlemaps->add_marker($marker);
    $data['map'] = $this->googlemaps->create_map();
    $this->_assign("map", $data['map']);
    $this->_assign("traList",$travelResult);
    $this->_fetch("admin_upload");
  }
  function upload() {
    $user = $this->ion_auth->user_array();
    $this->load->model('category_post_model','category_postModel');
    $whereTravel["name"]  = $this->input->post('name');
    $whereTravel["impression"]  = $this->input->post('impression');
    $whereTravel["content"] = $this->input->post('content');
    $whereTravel["comment_status"] = $this->input->post('comment_status');
    $whereTravel["tag_def"] = $this->input->post('tagDefault');
    $whereTravel["lat"]  = $this->input->post('lat');
    $whereTravel["lng"] = $this->input->post('lng');
    $whereTravel["update_date"] = $this->input->post('update_date');
    $whereTravel["cr_date"] = $this->input->post('cr_date');
    $whereTravel["lu_date"] = $this->input->post('lu_date');
    $whereTravel["status"] = "1";
    $whereTravel["usr_id"] = $user['id'];
    $whereTravel["usr_ip"] = $user['ip_address'];
    if(isset($_POST['upload'])) {
      $addResult = $this->travelModel->add($whereTravel);
      $post_id = mysql_insert_id();
      $whereTag["pos_id"] = $post_id;
      foreach ($whereTagTravel["cat_id"] as $key=>$val) {
      $whereTag["cat_id"] = $val;
      $addTag = $this->category_postModel->add($whereTag);
    }
       if(empty($addResult)){
        log_message("error","Can't add restaurant.");
       redirect(base_url("admin/restaurant"));
    }else{
      $this->load->model('images_model','imageModel');
      $gallery_path = realpath(APPPATH . '../themes/panhin/images/upload/');
      $config['upload_path'] = $gallery_path;
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';

      $this->load->library('upload', $config);
 
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['new_image'] = '';
      $configThumb['maintain_ratio'] = TRUE;
      $configThumb['width'] = 210;
      $configThumb['height'] = 140;

      $configThumb1 = array();
      $configThumb1['image_library'] = 'gd2';
      $configThumb1['source_image'] = '';
      $configThumb1['create_thumb'] = TRUE;
      $configThumb1['new_image'] = '';
      $configThumb1['maintain_ratio'] = TRUE;
      $configThumb1['width'] = 410;
      $configThumb1['height'] = 340;

     

      $this->load->library('image_lib');

      $whereImage["pos_id"] = $post_id;
      $first=0; 
      for($i = 1; $i < 7; $i++) {

        $upload = $this->upload->do_upload('image'.$i);

        if($upload === FALSE) continue;
     
        $data = $this->upload->data();

        $uploadedFiles[$i] = $data;

        if($data['is_image'] == 1) {
          $configThumb['source_image'] = $data['full_path'];
          $configThumb['new_image'] = $data['file_path']."/image_small";
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
          $configThumb1['source_image'] = $data['full_path'];
          $configThumb1['new_image'] = $data['file_path']."/image_medium";
          $this->image_lib->initialize($configThumb1);
          $this->image_lib->resize();
          $first++;
        if($first == 1) {
          $whereImage["firstimage"] = "1";
        }else{
          $whereImage["firstimage"] = "0";
        }
        $whereImage["filesize"] = $data['file_size'];
        $whereImage["filename"] = $data['file_name'];
        $whereImage["filepath"] = $data['file_path'];
        $whereImage["small"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["medium"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["large"] = $data['file_name'];
        $whereImage["fullpath"] = $data['full_path'];
        $whereImage["type"] = $data['image_type'];
        $addImg = $this->imageModel->add($whereImage);
           
        }
        
      }
    }
        redirect(base_url("admin/travel"));
    }
  }
  
  function show_detail($id=""){
    if(!empty($id)){
    $this->_assign("isDetail", TRUE);
    $this->load->model('travel_model','travelModel');
    $this->load->model('images_model','imagesModel');
    $where["where"]["id"] = $id;
    $whereImg["where"]["pos_id"] = $id;
    $whereReview["where"]["parent_id"] = $id;
    $travelResult = $this->travelModel->get($where);
    $travelImage = $this->imagesModel->get($whereImg);
    $travelReview = $this->travelModel->get($whereReview);
    foreach($restaurantReview as $key => $value){
    $whereImgRev["where"]["pos_id"] = $value['id'];
    $restaurantImgRev = $this->imagesModel->get($whereImgRev);
  } 
    $this->_assign("resList",$travelResult);
    $this->_assign("imgList",$travelImage); 
    $this->_assign("revList",$travelReview);
    $this->_assign("imgRevList",$travelImgRev);
    foreach($travelResult as $key => $tra )
      if(!empty($res["lat"]) and (!empty($res["lng"]))){
        $this->load->library('googlemaps');
        $config['center'] = $tra["lat"].','. $res["lng"];
        $config['zoom'] = '15';
        $config['map_height'] = '307px';
        $config['map_width'] = '307px';
        $this->googlemaps->initialize($config);

        $marker = array();
        $marker['position'] = $tra["lat"].','. $tra["lng"];
        $marker['title'] = $tra["impression"];
        $this->googlemaps->add_marker($marker);
        $data['map'] = $this->googlemaps->create_map();
        $this->_assign("map", $data['map']);
      }
    $this->_fetch("detail_restaurant");
    }
    else {
      redirect(base_url());
    }
 }
    function travelrequest_add(){
    $this->load->model("travel_model","travelModel");
    $where["where"]["status"] = "0";
    $where["where"]["type"] = "travel";
    $requestadd= $this->travelModel->get($where);
    $this->_assign('traveladd',$requestadd);
    $this->_fetch("travel_request_add");
  }

    function editstatustravel($id =""){
    $this->load->model("travel_model","travelModel");
    if(empty($id)){
      show_404();
    }
    $whereReport["where"]["id"] = $id;
    $data = $this->travelModel->get($whereReport);
    $this->_assign("status", $data[0]);
    $this->_fetch("changestatustravel");
 }
    function newstatustravel(){
    $this->load->model("travel_model","travelModel");
    $post=$this->input->post();
    $newstatus = $this->travelModel->add($post);

      if(!empty($newstatus)){
        redirect(base_url("admin/travel/request_add"));
      }else{
        show_error("Can't add record.");
      }
    }

    function travelrequest_del(){
    $this->load->model('travel_model','travelModel');
    $where["where"]["status"] = "2";
    $where["where"]["type"] = "travel";
    $requestadd= $this->travelModel->get($where);
    $this->_assign('delTravel',$requestadd);
    //var_dump($requestadd);exit();
    $this->_fetch("travel_request_del");
  }

   function traveldelete_request($id = ""){
    if(!$this->ion_auth->logged_in()){
      redirect(base_url("admin"),"refresh");
    }
    if(empty($id)){
      redirect(base_url("admin/travel"),"refresh");
    }
    $userdata = $this->ion_auth->user_array();
    $result = $this->travelModel->get($id);

    if($result[0]["usr_id"] == $userdata['id'] OR $this->ion_auth->in_group("admin")){
      //$delete["id"] = $id;
      //$delete["status"] = 0;
      $result = $this->travelModel->delete($id);
      //$this->cache->delete_group('post_');
    }else{
      show_404();
    }
    redirect(base_url("admin/travel/request_add"));
  }

  function changedelstatus($id=""){
    $this->load->model("travel_model","travelModel");
    if(!empty($id)){
        $whereReport["status"] = "0";
        $whereReport["where"]["id"] = $id;
        $data = $this->travelModel->update($whereReport);
        redirect(base_url("admin/travel/request_del"));
    }
    else{
    redirect(base_url("admin/travel"));
    }
  }

  function admin_search(){
    $this->_assign("adminIndex", TRUE);
    $this->load->model('travel_model','travelModel');
    $where["order"] = "pos_cr_date desc";
    $where["like"]["name"] = $this->input->post('name');
    $where["status"]  = "1";
    $where["where"]["img_firstimage"] = "1";
    $where["where"]["parent_id"] = "0";
    $travelResult = $this->travelModel->getImage($where);
    $this->_assign("traList",$travelResult);
    $this->_fetch("admin_index");
  }
  function user_review($id=""){
    if(!empty($id)){
    $this->load->model('travel_model','travelModel');
    $this->load->model('images_model','imagesModel');
    $where["where"]["id"] = $id;
    $travelResult = $this->travelModel->get($where);
    $this->_assign("resList",$travelResult);
    foreach($restaurantResult as $key => $res )
    $this->_fetch("user_review");
    }
    else {
      redirect(base_url());
    }
 }

 function user_review_upload() {
    $user = $this->ion_auth->user_array();
    $whereTra["name"]  = $this->input->post('name');
    $whereTra["parent_id"]  = $this->input->post('id');
    $whereTra["impression"]  = $this->input->post('impression');
    $whereTra["content"] = $this->input->post('content');
    $whereTra["comment_status"] = $this->input->post('comment_status');
    $whereTra["update_date"] = $this->input->post('update_date');
    $whereTra["cr_date"] = $this->input->post('cr_date');
    $whereTra["lu_date"] = $this->input->post('lu_date');
    $whereTra["status"] = "1";
    $whereTra["usr_id"] = $user['id'];
    $whereTra["usr_ip"] = $user['ip_address'];
    if(isset($_POST['upload'])) {
      $addResult = $this->travelModel->add($whereRest);
       if(empty($addResult)){
        log_message("error","Can't add restaurant.");
       redirect(base_url("home"));

    }else{
      $post_id = mysql_insert_id();
      $this->load->model('images_model','imageModel');
      $this->load->model('category_post_model','category_postModel');
      foreach($whereRest["tag"] as $key => $value){
        $tag['pos_id'] = $post_id;
        $tag['cat_id'] = $value;
        $addResult = $this->category_postModel->add($tag);
      }
      $gallery_path = realpath(APPPATH . '../themes/panhin/images/upload/');
      $config['upload_path'] = $gallery_path;
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';

      $this->load->library('upload', $config);
 
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['new_image'] = '';
      $configThumb['maintain_ratio'] = TRUE;
      $configThumb['width'] = 210;
      $configThumb['height'] = 140;

      $configThumb1 = array();
      $configThumb1['image_library'] = 'gd2';
      $configThumb1['source_image'] = '';
      $configThumb1['create_thumb'] = TRUE;
      $configThumb1['new_image'] = '';
      $configThumb1['maintain_ratio'] = TRUE;
      $configThumb1['width'] = 410;
      $configThumb1['height'] = 340;

     

      $this->load->library('image_lib');

      $whereImage["pos_id"] = $post_id;
      $first=0; 
      for($i = 1; $i < 5; $i++) {

        $upload = $this->upload->do_upload('image'.$i);

        if($upload === FALSE) continue;
     
        $data = $this->upload->data();

        $uploadedFiles[$i] = $data;

        if($data['is_image'] == 1) {
          $configThumb['source_image'] = $data['full_path'];
          $configThumb['new_image'] = $data['file_path']."/image_small";
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
          $configThumb1['source_image'] = $data['full_path'];
          $configThumb1['new_image'] = $data['file_path']."/image_medium";
          $this->image_lib->initialize($configThumb1);
          $this->image_lib->resize();
          $first++;
        if($first == 1) {
          $whereImage["firstimage"] = "1";
        }else{
          $whereImage["firstimage"] = "0";
        }
        $whereImage["title"]  = $this->input->post('taxt'.$i);
        $whereImage["filesize"] = $data['file_size'];
        $whereImage["filename"] = $data['file_name'];
        $whereImage["filepath"] = $data['file_path'];
        $whereImage["small"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["medium"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["large"] = $data['file_name'];
        $whereImage["fullpath"] = $data['full_path'];
        $whereImage["type"] = $data['image_type'];
        $addImg = $this->imageModel->add($whereImage);
           
        }
        
      }
    }
        redirect(base_url("home"));
    }
  }
}